# 爬取网页、下载音乐mp3到指定文件夹，仅用于学习研究，如有侵权，请联系删除
# author，mail to : lst136@qq.com
import re, requests
from lxml import etree

def get_url(dic):
    main_url = 'https://www.9ku.com/music/bdhot.htm'
    main_resp = requests.get(main_url)
    main_resp.close()
    main_resp.encoding = 'utf-8'
    main_tree = etree.HTML(main_resp.text)
    main_divs = main_tree.xpath('//*[@id="body"]/div[@class="songList clearfix"]')
    for div in main_divs:
        lis = div.xpath('./ol/li')
        for li in lis:
            href = 'https://m.9ku.com'+li.xpath('./a/@href')[0]
            name = li.xpath('./a/text()')[0]
            dic[name] = href


def down(url, name):
    resp = requests.get(url)
    resp.encoding = 'utf-8'
    resp.close()
    obj = re.compile(r'{mp3:"(?P<url>.*?)"}', re.S)
    real_url = obj.findall(resp.text)[0]
    i_header = {}
    i_header['Referer'] = 'https://www.9ku.com/'
    print('downlur:',real_url)
    real_resp = requests.get(real_url,headers=i_header)
    real_resp.close()
    file_name = 'f:/music/'+name+'.mp3'
    with open(file_name, 'wb') as f:
        f.write(real_resp.content)
    print(name, 'over')


if __name__ == '__main__':
    music_dic = {}
    get_url(music_dic)
    for item in music_dic.keys():
        try:
            down(music_dic.get(item), item)
        except:
            pass
    print('all over')
